package com.larry.hibernate.test;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

import com.larry.hibernate.vo.Book;
import com.larry.hibernate.vo.BookStore;

public class HibernateRunner {

	public static void main(String[] args) {
		Session session = new Configuration().configure().buildSessionFactory().openSession();
		HibernateRunner runner = new HibernateRunner();
		runner.search(session);
	}

	private void search(Session session) {
		Criteria criteria = session.createCriteria(Book.class);
		criteria.add(Restrictions.eq("bookStore.id", 3));
		List<Book> list = criteria.list();
		for (Book admin : list) {
			System.out.println(admin.getName());
		}
	}

	private void initData(Session session) {
		BookStore store = new BookStore();
		Set<Book> books = new HashSet<Book>();
		Book book1 = new Book();
		book1.setName("Spring 3.0");
		book1.setCtId("Larry");
		book1.setBookStore(store);
		books.add(book1);

		Book book2 = new Book();
		book2.setName("Struts 2");
		book2.setCtId("Larry");
		book2.setBookStore(store);
		books.add(book2);

		store.setBooks(books);
		store.setName("Larry Store");
		store.setTel("0987471365");
		store.setAddress("PEGA");
		store.setCtId("Larry");

		Transaction tx = session.beginTransaction();

		session.save(store);
		tx.commit();
		session.close();
	}
}
